
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test_demo/http/dio_export.dart';
import 'package:get/get_core/src/get_main.dart';
import 'package:get/get_instance/src/extension_instance.dart';

class HttpGetPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _HttpGetPage();

}
class _HttpGetPage extends State<HttpGetPage> {

  late HttpClient dio;
  late HttpClient dioOther;

  void get() async {
    HttpResponse appResponse = await dio.get("wxarticle/chapters/json");
    if (appResponse.ok) {
      debugPrint("====" + appResponse.data.toString());
    } else {
      debugPrint("====" + appResponse.error.toString());
    }
  }

  void other() async {
    HttpResponse appResponse = await dioOther.get("https://wanandroid.com/article/listproject/0/json");
    if (appResponse.ok) {
      debugPrint("====" + appResponse.data.toString());
    } else {
      debugPrint("====" + appResponse.error.toString());
    }
  }

  @override
  void initState() {
    super.initState();
    _initHttp();
    _initHttpOther();

    dio = Get.find<HttpClient>();
    dioOther = Get.find<HttpClient>(tag: 'three_http');
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('网络请求GET注入'),
      ),
      body: Column(children: [
        TextButton(
          onPressed: () {
            get();
          },
          child: Text('Get'),
        ),

        TextButton(
          onPressed: () {
            other();
          },
          child: Text('Other'),
        ),

      ],),
    );
  }

  /// app网络请求基本参数
  _initHttp(){
    HttpClient client = HttpClient(
      dioConfig: HttpConfig(baseUrl: "https://wanandroid.com/",
          // proxy: '192.168.1.9:8888',
          // headers: {
          //   'user-Agent': 'test',
          //   'version': '1',
          // },
        // queryParam: {
        //   'user-Agent': 'test-param',
        //   'version': '1-param',
        // },
      ),
    );
    Get.put<HttpClient>(client);
  }

  /// 第三方地址
  _initHttpOther(){
    HttpClient client = HttpClient(
      dioConfig: HttpConfig(
          // proxy: '192.168.1.9:8888',
      ),
    );
    Get.put<HttpClient>(client, tag: 'three_http');
  }

}